package cn.akwangl.spring.bean.definition;

import cn.akwangl.spring.bean.factory.DefaultUserFactory;
import cn.akwangl.spring.bean.factory.UserFactory;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * {@link SingletonBeanRegistry#registerSingleton} 以单例的方式注册Spring Bean Demo
 *
 * @author youyongkun
 * @date 2021/6/30 13:50
 */
public class SingletonBeanRegistrationDemo {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(SingletonBeanRegistrationDemo.class);

        SingletonBeanRegistry singletonBeanRegistry = applicationContext.getBeanFactory();
        UserFactory userFactory = new DefaultUserFactory();
        singletonBeanRegistry.registerSingleton("userFactory", userFactory);

        applicationContext.refresh();

        UserFactory userFactoryByLookup = applicationContext.getBean("userFactory", UserFactory.class);
        System.out.println("userFactory  == userFactoryByLookup : " + (userFactory == userFactoryByLookup));

        applicationContext.close();
    }

}
